package com.ruoyi.system.service.carte.job.impl;

import com.ruoyi.system.domain.XxkCarte;
import com.ruoyi.system.domain.request.BaseTransRequest;
import com.ruoyi.system.domain.request.JobExecuteRequest;
import com.ruoyi.system.domain.request.TransExecuteRequest;
import com.ruoyi.system.service.carte.AbstractCarteService;
import com.ruoyi.system.service.carte.CarteConst;
import com.ruoyi.system.service.carte.job.IJobExecute;
import com.ruoyi.system.service.carte.trans.ITransExecute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class JobExecuteImpl extends AbstractCarteService implements IJobExecute {
    @Override
    public String executeJob(JobExecuteRequest jobRequest) throws Exception {
        try{
            return commonHandle(jobRequest);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            throw e;
        }
    }

    public  void setRequestUrl(XxkCarte xxkCarte, BaseTransRequest baseTransRequest ) throws Exception {
        JobExecuteRequest transRequest =(JobExecuteRequest)baseTransRequest;
        if(StringUtils.isBlank(transRequest.getLevel())){
            transRequest.setLevel("Basic");
        }
        if(StringUtils.isBlank(transRequest.getRep())){
            transRequest.setRep("local");
        }
        String url = "http://" + xxkCarte.getCarteIp() + ":" + xxkCarte.getCartePort()+ CarteConst.JOB_EXECUTE_SUFFIXPATH;
        url = url.replace( " ", "%20" );
        transRequest.setRequestUrl(url);


        Map<String,Object> param=new HashMap<>();
        param.put("job",URLEncoder.encode( transRequest.getName(), "UTF-8" ));
        param.put("rep",transRequest.getRep());
        param.put("level",transRequest.getLevel());
        //此处可以设置扩展参数
        transRequest.setParmMap(param);
    }
}
